﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Tailor_Measurement : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loaddata();
        }
    }

    private void loaddata()
    {
        string sql = @"
SELECT [" + ddlItem.SelectedValue+@"ID] as ID
      ,[" + ddlItem.SelectedValue + @"Name] as Name   
  FROM Tailor_" + ddlItem.SelectedValue;

        DataSet ds = CommonManager.SQLExec(sql);
        gvItems.DataSource = ds.Tables[0];
        gvItems.DataBind();
    }

    protected void lbSelect_Click(object sender, EventArgs e)
    {
        LinkButton linkButton = new LinkButton();
        linkButton = (LinkButton)sender;
        
        string sql = @"
SELECT [" + ddlItem.SelectedValue + @"ID] as ID
      ,[" + ddlItem.SelectedValue + @"Name] as Name   
  FROM Tailor_" + ddlItem.SelectedValue + " where [" + ddlItem.SelectedValue + @"ID]=" + linkButton.CommandArgument;
        DataSet ds = CommonManager.SQLExec(sql);

        hfID.Value = ds.Tables[0].Rows[0][0].ToString();
        txtName.Text = ds.Tables[0].Rows[0][1].ToString();

    }
    protected void lbDelete_Click(object sender, EventArgs e)
    {
        LinkButton linkButton = new LinkButton();
        linkButton = (LinkButton)sender;
        
        string sql = "Delete [Tailor_" + ddlItem.SelectedValue + @"]   where [" + ddlItem.SelectedValue + @"ID]=" + linkButton.CommandArgument;
         
        CommonManager.SQLExec(sql);
        loaddata();
        
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        string sql="";
        if (hfID.Value == "")
        {
            sql = @"
INSERT INTO [Tailor_" + ddlItem.SelectedValue + @"]
           ([" + ddlItem.SelectedValue + @"Name]
           ,[ExtraField1]
           ,[ExtraField2]
           ,[ExtraField3]
           ,[ExtraField4]
           ,[ExtraField5])
     VALUES(N'" + txtName.Text + @"','','','','','')
";
        }
        else
        {
            sql = "update [Tailor_" + ddlItem.SelectedValue + @"] set [" + ddlItem.SelectedValue + @"Name]=N'"+txtName.Text+@"'  where [" + ddlItem.SelectedValue + @"ID]="+hfID.Value;
            hfID.Value = "";
        }
        txtName.Text = "";
        CommonManager.SQLExec(sql);
        loaddata();
    }
    protected void ddlItem_SelectedIndexChanged(object sender, EventArgs e)
    {
        loaddata();
    }
}